package com.cn.jdia.ets.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.gson.Gson;

@Controller
public class TicketsUtil {

	@RequestMapping("/ticket")
	public String dataCtrl(HttpServletRequest request, HttpServletResponse response) throws IOException {

		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		final String user = "biroot";
		
		//final String wgserver = "172.20.5.145:8000";
		final String wgserver = "172.20.5.203";

		String ticket = "";
		try {
			ticket = getTrustedTicket(wgserver, user, request.getRemoteAddr());
			System.out.println(request.getRemoteAddr());
			System.out.println("ticket:"+ticket);
		} catch (ServletException e) {
			e.printStackTrace();
		}

		Map<String, String> map = new HashMap<>();
		map.put("ticket", ticket);

		String kind = request.getParameter("kind");
		System.out.println(kind);

		// mapping
		if (kind.equals("keyuan")) {
			map.put("book", "_13");
			map.put("sheet", "sheet0");
		} else if (kind.equals("renli")) {
			map.put("book", "_0");
			map.put("sheet", "2");
		} else if (kind.equals("tingche")) {
			map.put("book", "_12");
			map.put("sheet", "sheet0");
		} else if (kind.equals("zichan")) {
			map.put("book", "_21");
			map.put("sheet", "sheet0");
		} else if (kind.equals("yanwu")) {
			map.put("book", "_2");
			map.put("sheet", "sheet2");
		} else if (kind.equals("anjian")) {
			map.put("book", "_8");
			map.put("sheet", "sheet0");
		} else if (kind.equals("hzlkl")) {
			map.put("book", "_6");
			map.put("sheet", "sheet0");
		} else if (kind.equals("ssyx")) {
			map.put("book", "_3");
			map.put("sheet", "sheet0");
		} else if (kind.equals("jchg")) {
			map.put("book", "_9");
			map.put("sheet", "sheet0_1");
		} else if (kind.equals("xlfx")) {
			map.put("book", "T2");
			map.put("sheet", "T2");
		} else if (kind.equals("xltj")) {
			map.put("book", "T2_2");
			map.put("sheet", "T2");
		} else if (kind.equals("sdyear")) {
			map.put("book", "_11");
			map.put("sheet", "sheet6");
		} else if (kind.equals("sdmonth")) {
			map.put("book", "-_0");
			map.put("sheet", "sheet0");
		} else if (kind.equals("zccx")) {
			map.put("book", "_20");
			map.put("sheet", "1");
		} else if (kind.equals("zcgl")) {
			map.put("book", "_19");
			map.put("sheet", "-?");
		} else if (kind.equals("zcbmtj")) {
			map.put("book", "-_3");
			map.put("sheet", "-?");
		} else if (kind.equals("tonghang")) {
			map.put("book", "_1");
			map.put("sheet", "1");
		} else if (kind.equals("jidi")) {
			map.put("book", "_17");
			map.put("sheet", "sheet0");
		} else if (kind.equals("sdtime")) {
			map.put("book", "-_1");
			map.put("sheet", "sheet0");
		} else if (kind.equals("jiaci")) {
			map.put("book", "_16");
			map.put("sheet", "sheet0");
		} else if (kind.equals("zgzs")) {
			map.put("book", "_4");
			map.put("sheet", "sheet0");
		} else if (kind.equals("bmfb")) {
			map.put("book", "_0");
			map.put("sheet", "sheet6");
		} else if (kind.equals("nlfb")) {
			map.put("book", "_0");
			map.put("sheet", "sheet7");
		} else if (kind.equals("peizai")) {
			map.put("book", "_10");
			map.put("sheet", "sheet3");
		} else if (kind.equals("xlgf")) {
			map.put("book", "T2_1");
			map.put("sheet", "T2_1");
		} else if (kind.equals("snky")) {
			map.put("book", "_13");
			map.put("sheet", "sheet1");
		} else if (kind.equals("srfx")) {
			map.put("book", "_18");
			map.put("sheet", "sheet0");
		} else if (kind.equals("gjhx")) {
			map.put("book", "10_0");
			map.put("sheet", "sheet0");
		} else if (kind.equals("ajtd")) {
			map.put("book", "_15");
			map.put("sheet", "sheet0");
		} else if (kind.equals("drhb")) {
			map.put("book", "_14");
			map.put("sheet", "sheet0");
		} else if (kind.equals("tbtj")) {
			map.put("book", "T2_0");
			map.put("sheet", "T2");
		} else if (kind.equals("zjyeb")) {
			map.put("book", "_7");
			map.put("sheet", "sheet0");
		} else if (kind.equals("jtsz")) {
			map.put("book", "_25");
			map.put("sheet", "sheet0");
		} else if (kind.equals("lssr")) {
			map.put("book", "_22");
			map.put("sheet", "sheet0");
		} else if (kind.equals("jtsr")) {
			map.put("book", "_23");
			map.put("sheet", "sheet0");
		} else if (kind.equals("szqk")) {
			map.put("book", "_24");
			map.put("sheet", "sheet0");
		} else if (kind.equals("qglkdt")) {
			map.put("book", "_28");
			map.put("sheet", "sheet0");
		} else if (kind.equals("snlkdt")) {
			map.put("book", "_27");
			map.put("sheet", "sheet0");
		}

		Gson gson = new Gson();
		String ret = gson.toJson(map);

		out.write(ret);
		out.flush();
		out.close();
		return null;
	}

	private String getTrustedTicket(String wgserver, String user, String remoteAddr) throws ServletException {
		OutputStreamWriter out = null;
		BufferedReader in = null;
		try {
			// Encode the parameters
			StringBuffer data = new StringBuffer();
			data.append(URLEncoder.encode("username", "UTF-8"));
			data.append("=");
			data.append(URLEncoder.encode(user, "UTF-8"));
			data.append("&");
			data.append(URLEncoder.encode("client_ip", "UTF-8"));
			data.append("=");
			data.append(URLEncoder.encode(remoteAddr, "UTF-8"));

			// Send the request
			URL url = new URL("http://" + wgserver + "/trusted");
			URLConnection conn = url.openConnection();
			conn.setDoOutput(true);
			out = new OutputStreamWriter(conn.getOutputStream());
			out.write(data.toString());
			out.flush();

			// Read the response
			StringBuffer rsp = new StringBuffer();
			in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			String line;
			while ((line = in.readLine()) != null) {
				rsp.append(line);
			}

			return rsp.toString();

		} catch (Exception e) {
			throw new ServletException(e);
		} finally {
			try {
				if (in != null)
					in.close();
				if (out != null)
					out.close();
			} catch (IOException e) {
			}
		}
	}
}
